package com.zoro.barn.business.service.system.business.team.controller;

import com.zoro.barn.business.service.system.business.team.entity.BarnTeam;
import com.zoro.barn.business.service.system.business.team.entity.BarnTeamUserDto;
import com.zoro.barn.business.service.system.business.team.service.BarnTeamService;
import com.zoro.barn.commons.webcommons.tools.WebContentUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/team")
public class BarnTeamController {

    private final BarnTeamService barnTeamService;

    public BarnTeamController(BarnTeamService barnTeamService) {
        this.barnTeamService = barnTeamService;
    }

    /**
     * 保存
     *
     * @param team data
     * @return saved data
     */
    @RequestMapping("/save")
    public BarnTeam save(@RequestBody BarnTeam team) {

        if (StringUtils.isBlank(team.getPid())) {
            // insert
            team.setCreateId(WebContentUtil.requestUserInfo().getPid());
            return this.barnTeamService.insert(team);
        } else {
            // update
            team.setUpdateId(WebContentUtil.requestUserInfo().getPid());
            return this.barnTeamService.update(team);
        }

    }

    /**
     * 主键查询
     *
     * @param pid pid
     * @return result
     */
    @RequestMapping("/getById")
    public BarnTeam getById(String pid) {
        return this.barnTeamService.getById(pid);
    }

    /**
     * 条件查询
     *
     * @param team 查询条件查询
     * @return 查询结果
     */
    @RequestMapping("/query")
    public List<BarnTeam> query(BarnTeam team) {
        return this.barnTeamService.query(team);
    }


    /**
     * 查询某个角team 的用户
     *
     * @param dto {@link BarnTeamUserDto}
     * @return result
     */
    @RequestMapping("/getUsers")
    public List<BarnTeamUserDto> getUsers(BarnTeamUserDto dto) {
        return this.barnTeamService.getUsers(dto);
    }
}
